package com.example.recharge;
import android.content.Context;
import android.content.SharedPreferences;
import android.widget.EditText;

import java.util.HashMap;
import java.util.Map;
class UserInfoUtil {
    private EditText editor;
    public static boolean saveUserInfo_androdi(Context mContext, String username, String password) {
        //保存用户密码
        try {
//            String userinfo=username+"##"+password;//封装用户名密码
//            String path="/data/data/com.example.recharge/";//指定保存路径
//           File file=new File(path,"password.txt");//创建file;
//            FileOutputStream fileOutputStream=new FileOutputStream(file);
//            fileOutputStream.write(userinfo.getBytes());//将用户名密码写入文件
//            fileOutputStream.close();
            SharedPreferences sharedPreferences=mContext.getSharedPreferences("Preservation_place",Context.MODE_PRIVATE);
            //通过SharedPreferences 对象获取一个Editor对象
            SharedPreferences.Editor editor=sharedPreferences.edit();
            editor.putString("username",username);
            editor.putString("password",password);
            //提交Editor线对
            editor.commit();
            return true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return false;
    }
//获取用户名密码
public static Map<String, String> getUserInfo_android(Context context){
    try {
        //1.通过Context对象创建一个SharedPreference对象
        SharedPreferences sharedPreferences=context.getSharedPreferences("Preservation_place",Context.MODE_PRIVATE);
        //2.通过sharedPreference获取存放的数据
        String username=sharedPreferences.getString("username","");
        String password=sharedPreferences.getString("password","");
//        String path = "/data/data/com.example.recharge/";//指定保存路径
//        //File file =new File(path,"password.txt");//创建file
//       File file=new File(path,"password.txt");
//       FileInputStream fileInputStream=new FileInputStream(file);
//        BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(fileInputStream));
//        //读取一行中包含用户密码，需要解析
//        String readLine=bufferedReader.readLine();
//        String[] split=readLine.split("##");
        HashMap<String,String>hashMap=new HashMap<String,String>();
        hashMap.put("username",username);
        hashMap.put("password",password);
//        hashMap.put("username",split[0]);
//        hashMap.put("password",split[1]);
//        bufferedReader.close();
//        fileInputStream.close();
        return hashMap;
    }catch (Exception e){
        e.printStackTrace();
    }
    return null;
}
}

